﻿#region License
//  Distrubted Under the GNU Public License version 2 (GPLv2)
// ========================================
// 
//  QuizzMasterStudio Version 3 
//  Copyright © 2012 , The Sparks (Autism App Jam 2014)
//  All rights reserved.  
// 
// Robert Long, Zephyr Yao, Hillary Chansavang, Li Xiao, Arial Xiao
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//  The full license is also included in the root folder.
// 
// Robert Long
//  Email: rbtLong@live.com
#endregion

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace Core.Util
{
    public static class ControlAnimationExtensionMethods
    {
        public static void FadeIn(this UIElement source, TimeSpan animTime)
        {
            var fadeInAnimation = new DoubleAnimation
            {
                From = 0,
                To = 1,
                Duration =  new Duration(animTime),
                EasingFunction = new PowerEase
                {
                    EasingMode = EasingMode.EaseIn,
                    Power = 1,
                },
            };
            Storyboard.SetTarget(fadeInAnimation, source);
            Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty));
            var sb = new Storyboard();
            sb.Children.Add(fadeInAnimation);
            sb.Begin();
        }

        public static void FadeOut(this UIElement source, int msAnimTime)
        {
            FadeOut(source, TimeSpan.FromMilliseconds(msAnimTime));
        }

        public static void FadeIn(this UIElement source, int msAnimTime)
        {
            FadeIn(source, TimeSpan.FromMilliseconds(msAnimTime));
        }

        public static void FadeOut(this UIElement source, TimeSpan animTime)
        {
            var fadeInAnimation = new DoubleAnimation
            {
                From = 1,
                To = 0,
                Duration = animTime,
                EasingFunction = new PowerEase
                {
                    EasingMode = EasingMode.EaseOut,
                    Power = 1,
                },
            };
            Storyboard.SetTarget(fadeInAnimation, source);
            Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty));
            var sb = new Storyboard();
            sb.Children.Add(fadeInAnimation);
            sb.Begin();
        }

        public static void DisplayTransitionInOut(this UIElement source, 
            TimeSpan transition, TimeSpan display)
        {
            source.FadeIn(transition);
            var tmr = new DispatcherTimer {Interval = display.Add(transition)};
            tmr.Tick += (sender, args) =>
            {
                source.FadeOut(transition);
                tmr.Stop();
                tmr.Tick -= delegate { };
            };
            tmr.Start();
        }

        public static Storyboard FindStoryBoardByKey(this UserControl source, string key)
        {
            return source.Resources[key] as Storyboard;
        }
    }
}
